package com.dragon.modules.yuegou.service.impl.merchant;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.exception.DragonException;
import com.dragon.modules.yuegou.client.ali.service.CheckBankNoService;
import com.dragon.modules.yuegou.entity.merchant.BankEntity;
import com.dragon.modules.yuegou.entity.merchant.UserEntity;
import com.dragon.modules.yuegou.enums.code.MerchantCode;
import com.dragon.modules.yuegou.mapper.merchant.BankMapper;
import com.dragon.modules.yuegou.model.merchant.Bank;
import com.dragon.modules.yuegou.query.merchant.BankQuery;
import com.dragon.modules.yuegou.service.merchant.BankService;
import com.dragon.modules.yuegou.service.merchant.UserService;
import com.dragon.modules.yuegou.web.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户银行卡 服务实现
 *
 * @author pbuilder generated
 * @date 2019-05-16 15:39:49
 * @version 2.0
 */
@Service("merBankService")
public class BankServiceImpl
		extends ServiceImpl<BankMapper, BankEntity>
		implements BankService {

	@Autowired
	private UserService userService;
	
	@Autowired
	private CheckBankNoService checkBankNoService;

	@Override
	public Bank getByMerchantUserId(String merchantUserId) {
		//获得商户用户信息
		UserEntity user = userService.getById(merchantUserId);
		BankQuery query = new BankQuery();
		query.setMerchantId(user.getMerchantId());
		query.setDel(false);
		List<BankEntity> list = list(query.toQueryWrapper());
		if(CollectionUtils.isNotEmpty(list)){
			return new Bank(list.get(0));
		}
		return new Bank();
	}

	@Override
	public List<Bank> bankList(String merchantId) {
		BankQuery query = new BankQuery();
		query.setMerchantId(merchantId);
		query.setDel(false);
		List<BankEntity> list = list(query.toQueryWrapper());
		list.stream().forEach(bank -> {
			StringBuilder sBuffe = new StringBuilder("**** **** **** ");
			sBuffe.append(bank.getEncBankNo().subSequence(bank.getEncBankNo().length()-4, bank.getEncBankNo().length()));
			bank.setEncBankNo(sBuffe.toString());
		});
		return list.stream().map(Bank::new).collect(Collectors.toList());
	}

	@Override
	public void bindingBank(Bank bank) {
		Check.notEmpty(bank.getEncBankNo(), "银行卡号不能为空");
		Check.notEmpty(bank.getEncTrueName(), "开户人名称不能为空");
		Check.notEmpty(bank.getBankCode(), "开户行不能为空");
		Check.notEmpty(bank.getBankName(), "银行名称不能为空");
		
		BankQuery query = new BankQuery();
		query.setMerchantId(bank.getMerchantId());
		query.setDel(false);
		Long count = count(query.toQueryWrapper());
		
		if (count > 0) {
			throw new DragonException(MerchantCode.BANK_NOT_REPEAT_BINGDING);
		}
		bank.del(false);

		//检测银行卡号和银行是否一致
		if(!checkBankNoService.checkCardDetail(bank.getEncBankNo(),bank.getBankName())){
			throw new DragonException(MerchantCode.CHECK_BANK_FAIL);
		}
		save(bank);
	}

	@Override
	public Bank getBank(String merchantId) {
		BankQuery query = new BankQuery();
		query.setMerchantId(merchantId);
		query.setDel(false);
		List<BankEntity> list = list(query.toQueryWrapper());
		return CollectionUtils.isEmpty(list)?new Bank():new Bank(list.get(0));
	}

	@Override
	public void deleteBank(String id) {
		Check.notEmpty(id, "银行卡id不能为空");
		
		BankEntity bank = getById(id);
		bank.setDel(true);
		bank.setDelTime(LocalDateTime.now());
		updateById(bank);
	}

	@Override
	public Bank withdrawBank(String merchantId) {
		BankQuery query = new BankQuery();
		query.setMerchantId(merchantId);
		query.setDel(false);
		List<BankEntity> list = list(query.toQueryWrapper());
		if (CollectionUtils.isNotEmpty(list)) {
			BankEntity bank = list.get(0);
			StringBuilder sBuffe = new StringBuilder("**** **** **** ");
			sBuffe.append(bank.getEncBankNo().subSequence(bank.getEncBankNo().length()-4, bank.getEncBankNo().length()));
			bank.setEncBankNo(sBuffe.toString());
			return new Bank(bank);
		}
		return new Bank();
	}
}